package com.leoman.spinningmachine.tab1;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.leoman.helper.util.AppManager;
import com.leoman.helper.util.LogUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.NewsDetail;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import butterknife.BindView;

/**
 * 详情
 */
public class Detail22Activity extends BaseActivity {

    @BindView(R.id.webView)
    WebView webview;
    private NewsDetail newsDetail;
    private int id;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_detail22;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        AppManager.getInstance().addActivity(this);
        initTitle("详情");
    }

    @Override
    protected void initData() {
        if(getIntent()!=null){
            id = getIntent().getIntExtra("flag", -1);
            LogUtil.json("id:"+id);
        }
        WebServiceApi.getInstance().newsDetail(SharedPreferencesUtils.getInstance().getString("token"),""+id,this,1);
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
         newsDetail = apiResponse.data.newsDetail;
         String content = newsDetail.getContent();

//        text(content);

        loadWebView(content);
    }

    private void loadWebView(String content) {
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.getSettings().setDisplayZoomControls(false);
        webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); //取消滚动条白边效果
        webview.setWebChromeClient(new WebChromeClient());
        webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setDefaultTextEncodingName("UTF-8") ;
        webview.getSettings().setBlockNetworkImage(false);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            webview.getSettings().setMixedContentMode(webview.getSettings()
                    .MIXED_CONTENT_ALWAYS_ALLOW);  //注意安卓5.0以上的权限
        }
        webview.loadDataWithBaseURL(null, getNewContent(content), "text/html", "UTF-8", null);
    }


    private String getNewContent(String htmltext){
        Document doc= Jsoup.parse(htmltext);
        Elements elements=doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width","100%").attr("height","auto");
        }
        return doc.toString();
    }

    private void text(String content) {
        LogUtil.json("::"+content);
        CharSequence charsequence= Html.fromHtml(content,new Html.ImageGetter(){
            @Override
            public Drawable getDrawable(String arg0) {
                Drawable drawable=Detail22Activity.this.getResources().getDrawable(R.mipmap.ic_launcher);
                //下面这句话不可缺少
                drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
                return drawable;
            }},null);
//        tv_detail.setText(charsequence);
    }
}
